package cn.cloud.all.security.oauth2.provider;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("serial")
public class TokenRequest extends BaseRequest {

    private String grantType;

    protected TokenRequest() {
    }

    public TokenRequest(Map<String, String> requestParameters, String clientId, Collection<String> scope, String grantType) {
        setClientId(clientId);
        setRequestParameters(requestParameters);
        setScope(scope);
        this.grantType = grantType;
    }

    public String getGrantType() {
        return grantType;
    }

    public void setGrantType(String grantType) {
        this.grantType = grantType;
    }

    public void setClientId(String clientId) {
        super.setClientId(clientId);
    }

    public void setScope(Collection<String> scope) {
        super.setScope(scope);
    }

    public void setRequestParameters(Map<String, String> requestParameters) {
        super.setRequestParameters(requestParameters);
    }

    public OAuth2Request createOAuth2Request(ClientDetails client) {
        Map<String, String> requestParameters = getRequestParameters();
        HashMap<String, String> modifiable = new HashMap<>(requestParameters);
        // Remove password if present to prevent leaks
        modifiable.remove("password");
        modifiable.remove("client_secret");
        // Add grant type so it can be retrieved from OAuth2Request
        modifiable.put("grant_type", grantType);
        return new OAuth2Request(modifiable, client.getClientId(), client.getAuthorities(), true, this.getScope(), client.getResourceIds(), null, null, null);
    }
}
